package com.users.struts2;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.region.entity.Region;
import com.region.service.IRegionService;
import com.users.entity.Users;
import com.users.service.IUsersService;

public class LoginAction extends ActionSupport {
	
	private static final long serialVersionUID = -4652227985671785460L;
	
	private String username;
	private String password;
	private Integer id;
	private String tip;
	private IUsersService ims;
	private IRegionService irs;
	private List<Region> regions;
	private List<Users> users;
	private Users user;
	private Integer totalPage;
	private Integer indexPage = 0; 
	
	
	public String login() throws Exception{
		if(ims.valid(getUsername(), getPassword())){
			regions = irs.findByRegionType(1);
			ActionContext ac = ActionContext.getContext();
			ac.getSession().put("user",username);
			setUsername("");
			return page();
		}
		else{
			return ERROR;
		}
	}
	
	public String page() throws Exception{
		users = ims.findAllUsers(indexPage);
		totalPage = ims.findAllUsers();
		return SUCCESS;
	}
	
	public String destorysession() throws Exception{
		ActionContext ac = ActionContext.getContext();
		ac.getSession().put("user", null);
		return SUCCESS;
	}
	
	public String editusers() throws Exception{
		user = ims.findUsersById(id);
		return SUCCESS;
	}
	
	public String updateusers() throws Exception{
		ims.updateUsers(user);
		page();
		return SUCCESS;
	}
	
	public String selectbyname() throws Exception{
		users = ims.selectByName(username,indexPage);
		totalPage = ims.findByUserName(username);
		return SUCCESS;
	}
	
	public String insertusers() throws Exception{
		ims.insertUsers(user);
		ActionContext ac = ActionContext.getContext();
		ac.getSession().put("user",user.getUsername());
		setUsername("");
		return page();
	}
	
	public String deleteusers() throws Exception{
		ims.deleteusers(id);
		return page();
	}
	
	public String execute() throws Exception{
		return SUCCESS;
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public IUsersService getIms() {
		return ims;
	}
	public void setIms(IUsersService ims) {
		this.ims = ims;
	}
	public IRegionService getIrs() {
		return irs;
	}
	public void setIrs(IRegionService irs) {
		this.irs = irs;
	}
	public List<Users> getUsers() {
		return users;
	}
	public void setUsers(List<Users> users) {
		this.users = users;
	}
	public List<Region> getRegions() {
		return regions;
	}
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}
	public Users getUser() {
		return user;
	}
	public void setUser(Users user) {
		this.user = user;
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public Integer getIndexPage() {
		return indexPage;
	}
	public void setIndexPage(Integer indexPage) {
		this.indexPage = indexPage;
	}
}
